/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhnb
 * Date: 2011-8-11 上午09:10:39
 */
package com.number.util.template;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Excel模板文件(*.ini)管理
 * @author zhnb
 *
 */
public class ExcelTemplateManager implements Serializable {

    private static final long serialVersionUID = 6523486495391291879L;

    /**
     * 读取指定模板文件为模板
     * @param templateFilename  文件名（不含路径）
     * @return
     */
    public List<String[]> getTemplate(String templateFilename){
        
        List<String[]> template = new ArrayList<String[]>();
        
        ExcelTemplateManager.class.getClassLoader();
        // 打开文件流
        InputStream inStream = ClassLoader.getSystemResourceAsStream(templateFilename);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inStream));
        
        // 按行读文件
        String currLine = null;
        try {
            while((currLine = bufferedReader.readLine()) != null){
                // 读取有效行（以'#'开头表示注释，去空格后长度为0表示空行）
                if ((!currLine.startsWith("#")) && (currLine.trim().length()>0)) {
                    // 第0行，SQL模板
                    if(currLine.startsWith("0")){
                        String[] currLineArr = currLine.split(";");
                        template.add(currLineArr);
                    }
                    // 第N行，各列定义
                    else {
                        String[] currLineArr = currLine.split(",");
                        template.add(currLineArr);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭文件流
            try {
                bufferedReader.close();
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return template;
    }
}
